﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using com.fukefu.source;

namespace com.fukefu.info.b2b.auto
{
    public class _99inf : Information
    {
        List<string> list = new List<string>();
        string user_name = "", user_pass = "";

        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("ecmsfrom", "");
            pd.Add("enews", "login");
            pd.Add("username", name);
            pd.Add("password", pass);
            pd.Add("lifetime", "0");
            pd.Add("Submit", "");

            string html = source.Post("http://www.99inf.com/e/member/login/", pd.Read(), "http://www.99inf.com/e/member/login/");
            if (html.IndexOf("<b>登录成功！</b>") != -1)
            {
                this.user_name = name;
                this.user_pass = pass;
                GetImages();
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult info = new InformationResult();
            string html = source.Get("http://www.99inf.com/e/DoInfo/AddInfo.php");
            if (html.IndexOf("请登录后再操作") != -1)
            {
                if (Login(user_name, user_pass, ""))
                {
                    info.Content = "自动重新登录成功";
                }
                else
                {
                    info.Content = "自动重新登录失败";
                    return info;
                }
            }
            content = MyFunction.ToText(content);
            ParametersData pd = new ParametersData("gb2312");
            pd.Add("title", title);
            pd.Add("type_id", "1");
            pd.Add("classid", site.TypeId[0]);
            pd.Add("class_id", site.TypeId[1]);
            pd.Add("summary", content.Substring(0, 150));
            pd.Add("title_pics", list[random.Next(list.Count)]);
            pd.Add("intro", content);
            pd.Add("expired_date", "2015/4/19");
            pd.Add("x", "45");
            pd.Add("y", "11");
            html = source.Post("http://www.99inf.com/e/DoInfo/AddInfo.php", pd.Read(), "http://www.99inf.com/e/DoInfo/AddInfo.php");
            if (html.IndexOf("<b>发布信息成功</b>") != -1)
            {
                html = source.Get("http://www.99inf.com/e/DoInfo/index.php?mod_id=" + site.TypeId[0]);
                if (Regex.IsMatch(html, "href=\"([^\"]+)\" title=\"" + title.Substring(0, (title.Length > 10 ? 10 : title.Length))))
                {
                    info.Code = 1;
                    info.Content = "http://www.99inf.com" + Regex.Match(html, "href=\"([^\"]+)\" title=\"" + title + "\"").Groups[1].Value;
                }
                else
                {
                    info.Code = 5;
                }
            }
            else if (html.IndexOf("今天发布信息量已满，请明天再来!") != -1)
            {
                info.Code = 3;
            }
            return info;
        }
        //获取所有图片
        public void GetImages()
        {
            string html = source.Get("http://www.99inf.com/e/DoInfo/product_list.php");
            MatchCollection mc = Regex.Matches(html, "height=\"102\" src=\"([^\"]+)\"");
            foreach (Match m in mc)
            {
                list.Add(m.Groups[1].Value);
            }
        }
    }
}
